<template>
    <div class="subjects">
        <topBar :title="title"></topBar>
        <div class="center">
            <div class="left">
                <div v-for="(item, index) in characterList" :key="index">
                    <div class="square"></div>
                    <div class="html">{{item.text}}</div>
                </div>
                <el-table
                    border
                    style="width: 100%"
                    :span-method="objectSpanMethod"
                    :cell-style="{padding: '0px'}"
                    :header-cell-style="{background: '#0070C0', color: '#ffffff',padding: '0',height:'26px'}"
                    :data="tableDataTwo">
                    <el-table-column
                        v-for="(item, index) in headerList"
                        :label="item.labelName"
                        :prop="item.propName"
                        :key="index"
                        align="center"
                    ></el-table-column>
                </el-table>
            </div>
            <div class="right">
                <el-table
                    border
                    style="width: 100%"
                    :span-method="objectMethod"
                    :cell-style="{padding: '0px'}"
                    :header-cell-style="{background: '#0070C0', color: '#ffffff',padding: '0',height:'26px'}"
                    :data="tableDataThree">
                    <el-table-column
                        v-for="(item, index) in headerArr"
                        :label="item.labelName"
                        :prop="item.propName"
                        :key="index"
                        align="center"
                        :width="item.width"
                    >
                    </el-table-column>
                </el-table>
            </div>
        </div>
    </div>
</template>

<script>
import topBar from "./topBar";
import {
  subjects,
} from "@/api/codeIncome/analysis";
export default {
    name: "customerType",
    components: {
        topBar
    },
    props: {
        searchData: {
        type: Object,
        default: {},
        },
    },
    watch: {
        searchData: {
        handler(newName) {
            this.getData();
        },
        deep: true,
        },
    },
    data(){
        return{
            title: '码号收入 – 按科目',
            characterList: [],
            tableDataTwo: [],
            headerList: [],
            spanArr: [],
            tableDataThree: [],
            headerArr: [],
            spanArr1: [],
            spanArr2: []
        }
    },
    created() {},
    mounted() {
        this.getData()
    },
    methods: {
        getData() {
            subjects({
        acctMonth: this.searchData.acctMonth,
        areaId: this.searchData.areaId,
      }).then((res) => {
                if (res.code == 200) {
                    this.characterList = res.data.characterList
                    let arr1 = []
                    let arr2 = []
                    res.data.dataList.map(item => {
                        if (item.code == 'P001') {
                            this.tableDataTwo = item.data
                            let contactDot = 0;
                            let spanArr = [];
                            this.tableDataTwo.forEach((item, index) => {
                                if (index === 0) {
                                    spanArr.push(1)
                                } else {
                                    if (item.subject1 === this.tableDataTwo[index - 1].subject1) {
                                        spanArr[contactDot] += 1;
                                        spanArr.push(0)
                                    } else {
                                        contactDot = index
                                        spanArr.push(1)
                                    }
                                }
                            })
                            this.spanArr = spanArr;
                            item.strList.map(its => {
                                if (its.code == 'header') {
                                    its.datas.map((it,index) => {
                                        let obj = {}
                                        let propArr = ['subject1','subject2','lastIncome','thisIncome','hbInrease']
                                        let widthArr = ['90px','90px','85px','85px','80px']
                                        obj['labelName'] = it
                                        obj['propName'] = propArr[index]
                                        obj['width'] = widthArr[index]
                                        arr1.push(obj)
                                    })
                                    this.headerList = arr1
                                }
                            })
                        }
                        if (item.code == 'P002') {
                            this.tableDataThree = item.data
                            let contactDot = 0;
                            let spanArr1 = [];
                            let contactDot1 = 0;
                            let spanArr2 = []
                            this.tableDataThree.forEach((item, index) => {
                                if (index === 0) {
                                    spanArr1.push(1)
                                    spanArr2.push(1)
                                } else {
                                    if (item.subject1 === this.tableDataThree[index - 1].subject1) {
                                        spanArr1[contactDot] += 1;
                                        spanArr1.push(0)
                                    }else {
                                        contactDot = index
                                        spanArr1.push(1)
                                    }
                                    if (item.subject2 === this.tableDataThree[index - 1].subject2) {
                                        spanArr2[contactDot1] += 1;
                                        spanArr2.push(0)
                                    }else {
                                        contactDot1 = index
                                        spanArr2.push(1)
                                    }
                                }
                            })
                            this.spanArr1 = spanArr1;
                            this.spanArr2 = spanArr2
                            item.strList.map(its => {
                                if (its.code == 'header') {
                                    its.datas.map((it,index) => {
                                        let obj = {}
                                        let propArr = ['subject1','subject2','subject3','lastIncome','thisIncome','hbInrease']
                                        let widthArr = ['','','200px','','','']
                                        obj['labelName'] = it
                                        obj['propName'] = propArr[index]
                                        obj['width'] = widthArr[index]
                                        arr2.push(obj)
                                    })
                                    this.headerArr = arr2
                                }
                            })
                        }
                    })
                }
            })
        },
        objectSpanMethod({ row, column, rowIndex, columnIndex }) {
            if (columnIndex === 0) {
                if(this.spanArr[rowIndex]){
                    return {
                        rowspan:this.spanArr[rowIndex],
                        colspan:1
                    }
                }else{
                    return {
                        rowspan: 0,
                        colspan: 0
                    }
                }
            }
        },
        objectMethod({ row, column, rowIndex, columnIndex }) {
            if (columnIndex === 0) {
                if(this.spanArr1[rowIndex]){
                    return {
                        rowspan:this.spanArr1[rowIndex],
                        colspan:1
                    }
                }else{
                    return {
                        rowspan: 0,
                        colspan: 0
                    }
                }
            }
            if (columnIndex === 1) {
                if (this.spanArr2[rowIndex]) {
                    return {
                        rowspan:this.spanArr2[rowIndex],
                        colspan:1
                    }
                } else {
                    return {
                        rowspan: 0,
                        colspan: 0
                    }
                }
            }
        },
    }
}
</script>

<style scoped>
.subjects {
    box-sizing: border-box;
    padding: 20px;
    box-shadow: 0px 0px 10px 1px rgb(0 0 0 / 15%);
}
</style>

<style scoped lang="scss">
.subjects{
    margin-top: 20px;
    height: 100%;
}
.center{
    box-sizing: border-box;
    padding: 0 20px;
    margin-top: 10px;
    .left{
        width: 100%;
        > div {
            display: flex;
            justify-content: flex-start;
            flex-wrap: wrap;
            margin-bottom: 20px;
        }
    }
    .right{
        width: 100%;
        > div {
            display: flex;
            justify-content: flex-start;
            flex-wrap: wrap;
            margin-bottom: 20px;
        }
    }
    .square{
        margin-top: 15px;
        margin-right: 10px;
        width: 10px;
        height: 10px;
        background: #c00000;
        transform: rotate(45deg);
    }
    .html {
        flex: 1;
        font-size: 18px;
        color: black;
        letter-spacing: 1px;
        line-height: 36px;
    }
}
</style>
